package com.zzptc.shop.dao;

import com.zzptc.shop.db.GoodIDB;
import com.zzptc.shop.db.IDB;
import com.zzptc.shop.vo.Users;
import com.zzptc.shop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao implements IDAO {


    @Override
    public boolean insert(VO vo) throws SQLException {
        GoodIDB db=new GoodIDB();
        Connection conn=null;
        PreparedStatement ps=null;
        int i=0;
        try{
            ps=db.getConn().prepareStatement
                    ("insert into easybuy_user" +
                            "(eu_user_id,eu_password)" +
                            " values(?,?)");
            Users users = (Users) vo;

            ps.setString(1,users.getEu_user_id()) ;

            ps.setString(1,users.getEu_user_name()) ;

            ps.setString(2,users.getEu_password());
            i=ps.executeUpdate();

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            {
                try {
                ps.close();
                db.closeAll(conn,ps,null);}catch (SQLException e){
                    e.printStackTrace();
                }
            }
            return i>0;
        }

    }

    @Override
    public void updata() throws SQLException {

    }

    @Override
    public boolean delete() throws SQLException {

        return false;
    }

    @Override
    public List<Users> getAll() {
        //定义集合，准备放Users对象
        List<Users> list=new ArrayList<Users>();
        IDB db=new GoodIDB();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs = null;
        try {
            conn=db.getConn();
            String sql="select * from easybuy_user";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            //循环遍历虚拟表 放入集合对象
            while (rs.next()){
                Users u =new Users();
                u.setEu_user_id(rs.getString(1));
                u.setEu_user_name(rs.getString(2));
                u.setEu_password(rs.getString(3));
                u.setEu_sex(rs.getString(4));
                u.setEu_birthday(rs.getString(5));
                u.setEu_identity_code(rs.getString(6));
                u.setEu_email(rs.getString(7));
                u.setEu_mobile(rs.getString(8));
                u.setEu_address(rs.getString(9));
                u.setEu_status(rs.getInt(10));
                list.add(u);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                db.closeAll(conn,ps,rs);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return list;
    }
    public boolean login(){
        IDB db=new GoodIDB();
        Users users=new Users();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        boolean i=false;
        try {
            conn=db.getConn();
            ps=conn.prepareStatement("select * from easybuy_user where eu_user_name=? and eu_password=?");
            ps.setString(1,users.getEu_user_name());
            ps.setString(2,users.getEu_password());
            rs=ps.executeQuery();
           i=rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }

    public static void main(String[] args) {
        //将list转换成json
        UserDao userDao=new UserDao();
        List<VO> listall=(ArrayList)userDao.getAll();
        //JSON包中的toJSONString方法     传入ArrayList集合
        System.out.println(listall.size());
    }
}